package com.hyyt.shopjsp.store.member.controller;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.store.member.pojo.ShopMemberShip;
import com.hyyt.shopjsp.store.member.service.IShopMemberShipService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/04/26 0024下午 15:23.
 */

@Controller
@RequestMapping("/back/memberShip")
public class ShopMemberShipController extends BaseController{
	
	private Logger log = Logger.getLogger(ShopMemberShipController.class);
	private String basePath = "/back/shop/customer/";

	@Resource
	private IShopMemberShipService shopMemberShipService;//店铺会员申请service

	/**
	* 跳转到店铺会员列表页
	 *
	* @return 返回到指定页面
	*/
	@RequestMapping("gotoMemberShipPage")
	public String gotoMemberShipPage(){
		return basePath+"listMemberShip";
	}

	/**
	* 查询商城与店铺信息
	* @params page
	* @params response
	* @params request
	*/
	@RequestMapping(value="listMemberShip",method=RequestMethod.POST)
	@ResponseBody
	public void listMemberShip(String shopName,String state,Page page, HttpServletResponse response){
		Map<String, Object> params = new HashMap<>();
		if(Utils.objectIsNotEmpty(shopName)){
			params.put("shopName", shopName.trim());
		}
		if(Utils.objectIsNotEmpty(state)){
			params.put("state", state);
		}
		params.put("sortColumns", "memberShipId desc");
		int totalRecordCount =shopMemberShipService.countShopMemberShip(params);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<ShopMemberShip> memberShipList =shopMemberShipService.queryShopMemberShip(params, page.getPageRecordBeginIndex(), page.getPageSize());
		params.clear();
		params.put("total", totalRecordCount);// total键 存放总记录数，必须的
		params.put("rows", memberShipList);// rows键 存放每页记录 list
		ResultMsg.create(params, response);  //把内容发送到页面
	}

	/**
	 *  保存店铺会员审核
	 *
	 * @param memberShipId	店铺会员申请ID
	 * @param discount		会员折扣比例
	 * @param state			审核状态
	 * @return
	 */
	@RequestMapping(value = "saveOrUpdateAuditState", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg saveOrUpdateAuditState(String memberShipId,String discount,String state) {
		boolean flag=false;
		if (Utils.objectIsNotEmpty(memberShipId) && Utils.objectIsNotEmpty(state)) {
			ShopMemberShip shopMemberShip = shopMemberShipService.loadShopMemberShip(Integer.parseInt(memberShipId));
			if(Utils.objectIsNotEmpty(shopMemberShip)&&Utils.objectIsNotEmpty(shopMemberShip.getMemberShipId())){
				shopMemberShip.setState(Integer.valueOf(state));
				if(Utils.objectIsNotEmpty(discount)){
					shopMemberShip.setDiscount(BigDecimal.valueOf(Double.valueOf(discount)));
				}else {
					shopMemberShip.setDiscount(BigDecimal.valueOf(0));
				}
				Timestamp date =new Timestamp(System.currentTimeMillis());//获取当前系统timestamp
				shopMemberShip.setAuditTime(date);
				shopMemberShipService.updateShopMemberShip(shopMemberShip);
				flag=true;
			}
			return ResultMsg.create(flag);
		}else {
			return ResultMsg.create(false);
		}
	}

}
